//
//  ViewController.m
//  MJRefresh
//
//  Created by yutao on 15/2/3.
//  Copyright (c) 2015年 yutao. All rights reserved.
//

#import "ViewController.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
#import "Person.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.allFriendsArray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    self.isUpLoading = NO;

    // 2.集成上拉下拉功能
    [self addHeader];
    [self addFooter];
    
    
    
}

#pragma mark --下拉刷新
- (void)addHeader
{
//    __unsafe_unretained typeof(self) vc = self;
    
    __block ViewController *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量

    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage = 1;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc GetPersonList:vc.nextPage];//重新请求数据

    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.myTableView headerBeginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
//    __unsafe_unretained typeof(self) vc = self;
    __block ViewController *vc = self;

    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage ++;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc GetPersonList:vc.nextPage];//请求数据

    }];
}





#pragma mark --使用AFN进行数据请求
-(void)GetPersonList:(NSInteger)nextCursor
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    //****参数cursor的值: 第一页默认为0，下一页就是请求下来数据中next_cursor的值
    //****参数count 的值: 是指定一页显示多少条数据
    
//    NSString *url_string = [NSString stringWithFormat:@"https://api.weibo.com/2/friendships/friends.json?uid=5626008324&access_token=2.00klJkIGGZulFC50e2f524e40S8nlw&cursor=%ld&count=2",nextCursor];
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://watch-cdn.idailywatch.com/api/list/video/zh-hans?page=%ld&ver=iphone&app_ver=9",nextCursor];

    NSLog(@" page = %ld\n url = %@", self.nextPage, url_string);
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        
        
        NSMutableArray *allArray = responseObject;
        NSLog(@"使用AFN进行get请求 ===  %@",allArray);
        
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.allFriendsArray removeAllObjects];
        }
        
        for (NSDictionary *dic  in allArray) {
            
            Person *person = [[Person alloc] init];
            NSString *string = [NSString stringWithFormat:@"%@",[dic objectForKey:@"guid"]];
            person.personName = string;
            //使用title会有一行是不显示的,因为最后一条数据没有title关键字
            person.title = [dic objectForKey:@"title"];
            [self.allFriendsArray addObject:person];
            
        }
        
        
        [self.myTableView footerEndRefreshing];
        [self.myTableView headerEndRefreshing];
        [self.myTableView reloadData];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return  self.allFriendsArray.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    /*
     * Content in this cell should be inset the size of kMenuOverlayWidth
     */
    
    Person *p = [self.allFriendsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = p.personName;
    cell.detailTextLabel.text = p.title;
    
    return cell;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
